const router = require('koa-router')()
const ADMIN = require("../app/admin")
router.prefix('/admin')
const AUTH = require("../auth/auth.js")
const multer = require('koa-multer');
const AD = require("../app/ad.js")
const MENU = require("../app/menu.js")
const CATEGORY = require("../app/category.js")
const GOODS = require("../app/goods.js")
const ORDER = require("../app/order.js")
router.get("*", AUTH.checkHeader)
router.post('/', ADMIN.login)

router.get('/getuser', ADMIN.getAdminList)
router.post('/adduser', ADMIN.addUser)
router.post('/edituser', ADMIN.editUser)
router.get('/deluser', ADMIN.delUser)

// 角色
router.get('/rolelist', ADMIN.getRoleList)
router.get('/roleuserlist', ADMIN.getRoleUserList)
router.post('/addusertorole', ADMIN.addUserToRole)
router.get('/deluserfromrole', ADMIN.delUserFromRole)
router.post('/addrole', ADMIN.addRole)
router.post('/editrole', ADMIN.editRole)
router.get('/delrole', ADMIN.delRole)

// 权限
router.get('/accesslist', ADMIN.getAccessList)
router.post('/addaccess', ADMIN.addAccess)
router.post('/editaccess', ADMIN.editAccess)
router.get('/delaccess', ADMIN.delAccess)
router.get('/getroleaccess', ADMIN.getRoleAccess)
router.post('/addroleaccess', ADMIN.addRoleAccess)
// 动态路由
router.get("/getPermission", ADMIN.getPermission)

// 广告
router.get('/getAd', AD.geAd)
router.post('/addAd', AD.addAd)
router.post('/editAd', AD.editAd)
router.get('/delAd', AD.delAd)
// 广告位
router.get('/getAdPosition', AD.geAdPosition)
router.post('/addAdPosition', AD.addAdPosition)
router.post('/editAdPosition', AD.editAdPosition)
router.get('/delAdPosition', AD.delAdPosition)

// 菜单接口MENU
router.get('/getMenu', MENU.getMenu)
router.post('/addMenu', MENU.addMenu)
router.post('/editMenu', MENU.editMenu)
router.get('/delMenu', MENU.delMenu)


// CATEGORY 分类接口
router.get('/getCategory', CATEGORY.getCategory)
router.post('/addCategory', CATEGORY.addCategory)
router.post('/editCategory', CATEGORY.editCategory)
router.get('/delCategory', CATEGORY.delCategory)
// GOODS

router.get('/getGoods', GOODS.getGoodsList)
router.post('/addGoods', GOODS.addGoods)
router.post('/editGoods', GOODS.editGoods)
router.get('/delGoods', GOODS.delGoods)

// 订单 列 表 ORDER
 router.get('/getOrderList', ORDER.getOrderList)
 router.post('/tuiKuan', ORDER.tuiKuan)
 router.post('/heXiao', ORDER.heXiao)
//配置    
var storage = multer.diskStorage({
	//文件保存路径
	destination: function(req, file, cb) {
		cb(null, 'public/uploads/') //注意路径必须存在
	},
	//修改文件名称
	filename: function(req, file, cb) {
		var fileFormat = (file.originalname).split(".");
		cb(null, Date.now() + "." + fileFormat[fileFormat.length - 1]);
	}
})
//加载配置
var upload = multer({
	storage: storage
})

router.post('/upload', upload.single('file'), async (ctx, next) => {
	ctx.body = {
		code: 0,
		codeInfo: "成功",
		data: {
			filename: '/uploads/' + ctx.req.file.filename, //返回文件名
		}
	}
});

router.post('/wangEditUpload', upload.single('file'), async (ctx, next) => {
	ctx.body = {
		"errno": 0, // 注意：值是数字，不能是字符串
		"data": {
			"url": 'http://127.0.0.1:3000/uploads/'+ctx.req.file.filename, // 图片 src ，必须
			"alt": "yyy", // 图片描述文字，非必须
			"href": "zzz" // 图片的链接，非必须
		}
	}

	// {
	// 	code: 0,
	// 	codeInfo: "成功",
	// 	data: {
	// 		filename: '/uploads/'+ctx.req.file.filename, //返回文件名
	// 	}
	// }
});
module.exports = router